package com.cooba.activitys.setting;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.cooba.R;
import com.cooba.activitys.MainActivity;
import com.cooba.activitys.login.LoginActivity;
import com.cooba.activitys.user.UserInfoActivity;
import com.cooba.utils.MySqlOpenHelper;
import com.cooba.utils.ServerURL;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
/**
 * Created by Administrator on 2015/12/15.
 * 设置
 */
public class SettingActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView mMe_image_back;
    private RelativeLayout mUserinfo;
    private Button mBt_exit;
    private MySqlOpenHelper helper;
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        mMe_image_back = (ImageView) findViewById(R.id.set_image_back);
        mUserinfo = (RelativeLayout) findViewById(R.id.userinfo);
        mBt_exit = (Button) findViewById(R.id.bt_exit);
        mMe_image_back.setOnClickListener(this);
        mUserinfo.setOnClickListener(this);
        mBt_exit.setOnClickListener(this);
        getCount();
    }

    //读取数据
    private void getCount() {

        helper = new MySqlOpenHelper(getApplication());
        db = helper.getReadableDatabase();
        Cursor cursor = db.query("login_date", null, null, null, null, null, null, null);
        if (cursor.moveToFirst()) {

        } else {
            mBt_exit.setVisibility(View.INVISIBLE);
        }
        cursor.close();
        db.close();
    }

    @Override
    public void onClick(View v) {

        Intent intent;
        switch (v.getId()) {
            case R.id.set_image_back:
                intent = new Intent(getApplication(), MainActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.userinfo:
                helper = new MySqlOpenHelper(getApplication());
                db = helper.getReadableDatabase();
                Cursor cursor = db.query("login_date", null, null, null, null, null, null, null);
                if (cursor.moveToFirst()) {
                    intent = new Intent(getApplication(), UserInfoActivity.class);
                    startActivity(intent);

                } else {
                    intent = new Intent(getApplication(), LoginActivity.class);
                    startActivity(intent);
                }
                cursor.close();
                db.close();
                break;
            //安全退出
            case R.id.bt_exit:
                helper = new MySqlOpenHelper(getApplication());
                db = helper.getReadableDatabase();
                Cursor cursor1 = db.query("login_date", null, null, null, null, null, null, null);
                if (cursor1.moveToFirst()) {
                    String ph = cursor1.getString(cursor1.getColumnIndex("phone"));
                    HttpUtils httpUtils = new HttpUtils();
                    RequestParams params = new RequestParams();
                    params.addBodyParameter("phone", ph);
                    httpUtils.send(HttpRequest.HttpMethod.POST, ServerURL.httpUrl + ServerURL.Logout, params, new RequestCallBack<String>() {
                        @Override
                        public void onFailure(HttpException arg0, String arg1) {
                        }

                        @Override
                        public void onSuccess(ResponseInfo<String> responseInfo) {
                            String status = JSON.parseObject(responseInfo.result).getString("status");

                            Log.e("ssssss", "status=" + status);
                            if (status.equals("0")) {
                                Toast.makeText(getApplication(), "退出失败", Toast.LENGTH_SHORT).show();
                            } else {
                                Intent intent = new Intent(getApplication(), MainActivity.class);
                                startActivity(intent);
                                db.delete("login_date", null, null);
                                helper.close();
                            }

                        }

                    });
                }


                break;


        }
    }
}
